// pages/news-list/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: app.data.api.root_host,
    isCard: true,
    currentContent: '',
    dataList: []

  },
  textareaBInput: function(e){
    var index = e.currentTarget.dataset.index;
    var pindex = e.currentTarget.dataset.pindex;
    var content = e.detail.value;
    console.log(content);
    this.setData({
      currentContent: content
    })
    var dataList = this.data.dataList;
    var p_id = '';
    console.log(pindex)
    if(pindex == -1){
      p_id = dataList[index].id;
    }else{
     p_id = dataList[pindex]['commentList'][index]['id'];
    }
  },
  //提交评价
  submitComment: function(e){
    console.log(e);
    if(!this.data.currentContent){
      wx.showToast({
        title: '请输入评价内容',
        icon: 'none'
      })
      return;
    }
    var currentData = this.getCommentData(e);
    this.changeItemStatus(e);
    app.data.utils.isLogin();
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    app.data.utils.request({
      url: app.data.api.posts_action,
      data: {
        members_id: userInfo.id, 
        images: [], 
        title: '',
        content: this.data.currentContent,
        nickname: userInfo.nickname,
        parent_id: currentData.id,
      },
      method: 'post',
      success: (res)=> {
        wx.showToast({
          title: '评价成功',
        })
        this.getList();
      }
    })
  },
  commentAction: function(e){
    this.changeItemStatus(e);
  },
  getCommentData: function(e){
    var index = e.currentTarget.dataset.index;
    var pindex = e.currentTarget.dataset.pindex;
    var dataList = this.data.dataList;
    var comment_data = {};
    if(pindex == -1){
      comment_data = dataList[index];
    }else{
      console.log(pindex)
      console.log( dataList[pindex]);
      comment_data = dataList[pindex]['commentList'][index];
    }
    return comment_data;
  },
  changeItemStatus: function(e){
    var index = e.currentTarget.dataset.index;
    var pindex = e.currentTarget.dataset.pindex;
    var dataList = this.data.dataList;
    console.log(pindex)
    if(pindex == -1){
      dataList[index]['commentStatus'] = !dataList[index]['commentStatus'];
      this.setData({
        dataList: dataList
      })
    }else{
      console.log(pindex)
      console.log( dataList[pindex]);
      dataList[pindex]['commentList'][index]['commentStatus'] = !dataList[pindex]['commentList'][index]['commentStatus'];
      this.setData({
        dataList: dataList
      })
    }
  },
  openAction: function(e){
    console.log(e);
    var index = e.currentTarget.dataset.index
    var dataList = this.data.dataList;
    dataList[index].open_status = !dataList[index].open_status;
    this.setData({
      dataList: dataList
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList();
  },
  getList: function(){
    app.data.utils.request({
      url: app.data.api.posts_list,
      data: {},
      method: 'get',
      success: (data) => {
        this.setData({
          dataList: data,
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  zanAction: function(e) {
    var posts_id = e.currentTarget.dataset.posts_id;
    app.data.utils.request({
      url: app.data.api.posts_zan+"/"+posts_id+"/zan",
      data: {
      },
      method: 'post',
      success: (res)=> {
        wx.showToast({
          title: '点赞成功',
        })
        this.getList();
      }
    })
  }
})